package pl.mess.core.tags.elements;

import pl.mess.core.uiobjects.MESSNewsEntry;
import pl.mess.core.tags.ifc.ControlAttribute;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gibffe
 * Date: Jun 16, 2006
 * Time: 12:34:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class MESSNewsViewTag extends MESSBaseTag {

    private static final String TAG_NAME = "newsView";

    /**
     * paremeter defining request scope object name containing tree data
     */
    @ControlAttribute public String source = null;


    public String getTagName() {
        return TAG_NAME;
    }

    /**
    * StartTag method
    * @return
    * @throws javax.servlet.jsp.JspException
    */
    public int doStartTag() throws JspException {

        super.doStartTag();

        // get tree source from scope
        ServletRequest request = this.getPageContext().getRequest();
        List news = (List) request.getAttribute(this.source);

        for (Iterator it = news.iterator(); it.hasNext();) {
            // recursively render tree elements
            MESSNewsEntry el = (MESSNewsEntry) it.next();
            this.writeNewsEntry(el);
        }


        return this.evalBody();
    }

    /**
     * Recursively render tree element
     * @param element
     */
    private void writeNewsEntry(MESSNewsEntry element) {

        MESSNewsViewElementTag elTag = new MESSNewsViewElementTag(this.getWriter(), element);

        // write header
        elTag.writeTagStart();
        // write tail
        elTag.writeTagEnd();
    }

    /**
    * EndTag method
    * @return
    * @throws JspException
    */
    public int doEndTag() throws JspException {

        return super.doEndTag();
    }


    // parameter: source
    public void setSource(String source) {
        this.source = source;
    }
}
